package com.easylinkin.linkappapi.common.model;

import cn.hutool.core.util.StrUtil;
import com.easylinkin.core.models.IOperator;
import com.easylinkin.core.models.Operator;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.exceptions.ExceptionEnum;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import lombok.Setter;

import javax.annotation.Resource;

/**
 * @author kyj
 * @date 2024/5/17 16:06
 */
@Setter(onMethod_ = @Resource)
public class BaseController {

    private LinkappUserContextProducer linkappUserContextProducer;

    public Operator getOperator() {
        LinkappUser current = linkappUserContextProducer.getCurrent();
        if (current == null || StrUtil.isEmpty(current.getTenantId())) {
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.EXCEPTION_CODE_500, "未获取到当前用户信息");
        }

        Operator operator = new Operator();
        operator.setUserId(String.valueOf(current.getId()));
        operator.setUsername(current.getUsername());
        operator.setNickname(current.getNickname());
        operator.setTenantId(current.getTenantId());

        return operator;
    }

    public void setOperator(IOperator iOperator) {
        iOperator.setOperator(getOperator());
    }
}
